"use strict";

const Controller = require("egg").Controller;

class GoodsController extends Controller {
  //列表查看
  async goodsList() {
    // console.log(user);
    const { ctx } = this;
    const { pageIndex, pageSize } = ctx.request.body;
    const sqlData = await ctx.service.goods.goodsList(pageIndex, pageSize);
    ctx.body = {
      code: 200,
      list: sqlData.res,
      total: sqlData.total.length,
      // total: sqlData.length,
    };
  }
  // 添加
  async goodsAdd() {
    const { ctx } = this;
    const { name } = ctx.request.body;
    await ctx.service.goods.goodsAdd(name);
    ctx.body = {
      code: 200,
      msg: "添加成功",
    };
  }
  // 删除
  async goodsDel() {
    const { ctx } = this;
    const { id } = ctx.query;
    await ctx.service.goods.goodsDel(id);
    ctx.body = {
      code: 200,
      msg: "删除成功",
    };
  }
  //编辑
  async goodsSet() {
    const { ctx } = this;
    const { id, name } = ctx.request.body;
    await ctx.service.goods.goodsSet(id, name);
    ctx.body = {
      code: 200,
      msg: "编辑成功",
    };
  }
}

module.exports = GoodsController;
